﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//Please add references
namespace Chizx.DAL
{
	/// <summary>
	/// 数据访问类:user
	/// </summary>
	public partial class UserDal
	{
		public UserDal()
		{}
		#region  BasicMethod

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperSQL.GetMaxID("zc_id", "z_user"); 
		}

       

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(string zu_id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from z_user");
			strSql.Append(" where zu_id=@ChizxFDzu_id ");
			SqlParameter[] parameters = {
					new SqlParameter("@ChizxFDzu_id", SqlDbType.VarChar,50)};
			parameters[0].Value = zu_id;
			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public bool Add(Chizx.Model.UserModel model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into z_user(");
			strSql.Append("zu_id,zc_id,zu_name,zu_pwd,zu_sex,zu_roles,zu_weixin,zu_qq,zu_tel,zu_email,zu_lastdate,zu_lastip,zu_createdate,zu_status,zu_direct)");
			strSql.Append(" values (");
            strSql.Append("@ChizxFDzu_id,@ChizxFDzc_id,@ChizxFDzu_name,@ChizxFDzu_pwd,@ChizxFDzu_sex,@ChizxFDzu_roles,@ChizxFDzu_weixin,@ChizxFDzu_qq,@ChizxFDzu_tel,@ChizxFDzu_email,@ChizxFDzu_lastdate,@ChizxFDzu_lastip,@ChizxFDzu_createdate,@ChizxFDzu_status,@ChizxFDzu_direct)");
			SqlParameter[] parameters = {
					new SqlParameter("@ChizxFDzu_id", SqlDbType.VarChar,50),
					new SqlParameter("@ChizxFDzc_id", SqlDbType.Int,4),
					new SqlParameter("@ChizxFDzu_name", SqlDbType.VarChar,30),
					new SqlParameter("@ChizxFDzu_pwd", SqlDbType.VarChar,30),
					new SqlParameter("@ChizxFDzu_sex", SqlDbType.TinyInt,1),
					new SqlParameter("@ChizxFDzu_roles", SqlDbType.Char,2),
					new SqlParameter("@ChizxFDzu_weixin", SqlDbType.VarChar,30),
					new SqlParameter("@ChizxFDzu_qq", SqlDbType.VarChar,30),
					new SqlParameter("@ChizxFDzu_tel", SqlDbType.VarChar,30),
					new SqlParameter("@ChizxFDzu_email", SqlDbType.VarChar,40),
					new SqlParameter("@ChizxFDzu_lastdate", SqlDbType.VarChar,20),
					new SqlParameter("@ChizxFDzu_lastip", SqlDbType.VarChar,20),
					new SqlParameter("@ChizxFDzu_createdate", SqlDbType.DateTime),
					new SqlParameter("@ChizxFDzu_status", SqlDbType.Char,2),
                    new SqlParameter("@ChizxFDzu_direct", SqlDbType.VarChar,50)};
			parameters[0].Value = model.zu_id;
			parameters[1].Value = model.zc_id;
			parameters[2].Value = model.zu_name;
			parameters[3].Value = model.zu_pwd;
			parameters[4].Value = model.zu_sex;
			parameters[5].Value = model.zu_roles;
			parameters[6].Value = model.zu_weixin;
			parameters[7].Value = model.zu_qq;
			parameters[8].Value = model.zu_tel;
			parameters[9].Value = model.zu_email;
			parameters[10].Value = model.zu_lastdate;
			parameters[11].Value = model.zu_lastip;
			parameters[12].Value = model.zu_createdate;
			parameters[13].Value = model.zu_status;
            parameters[14].Value = model.zu_direct;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(Chizx.Model.UserModel model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update z_user set ");
			strSql.Append("zu_name=@ChizxFDzu_name,");
			strSql.Append("zu_pwd=@ChizxFDzu_pwd,");
			strSql.Append("zu_sex=@ChizxFDzu_sex,");
			strSql.Append("zu_roles=@ChizxFDzu_roles,");
			strSql.Append("zu_weixin=@ChizxFDzu_weixin,");
			strSql.Append("zu_qq=@ChizxFDzu_qq,");
			strSql.Append("zu_tel=@ChizxFDzu_tel,");
			strSql.Append("zu_email=@ChizxFDzu_email,");
			strSql.Append("zu_lastdate=@ChizxFDzu_lastdate,");
			strSql.Append("zu_lastip=@ChizxFDzu_lastip,");
			strSql.Append("zu_createdate=@ChizxFDzu_createdate,");
            strSql.Append("zu_status=@ChizxFDzu_status,");
            strSql.Append("zu_direct=@ChizxFDzu_direct");
			strSql.Append(" where zu_id=@ChizxFDzu_id and zc_id=@ChizxFDzc_id ");
			SqlParameter[] parameters = {
					new SqlParameter("@ChizxFDzu_name", SqlDbType.VarChar,30),
					new SqlParameter("@ChizxFDzu_pwd", SqlDbType.VarChar,30),
					new SqlParameter("@ChizxFDzu_sex", SqlDbType.TinyInt,1),
					new SqlParameter("@ChizxFDzu_roles", SqlDbType.Char,2),
					new SqlParameter("@ChizxFDzu_weixin", SqlDbType.VarChar,30),
					new SqlParameter("@ChizxFDzu_qq", SqlDbType.VarChar,30),
					new SqlParameter("@ChizxFDzu_tel", SqlDbType.VarChar,30),
					new SqlParameter("@ChizxFDzu_email", SqlDbType.VarChar,40),
					new SqlParameter("@ChizxFDzu_lastdate", SqlDbType.VarChar,20),
					new SqlParameter("@ChizxFDzu_lastip", SqlDbType.VarChar,20),
					new SqlParameter("@ChizxFDzu_createdate", SqlDbType.DateTime),
					new SqlParameter("@ChizxFDzu_status", SqlDbType.Char,2),
                    new SqlParameter("@ChizxFDzu_direct", SqlDbType.VarChar,30),
					new SqlParameter("@ChizxFDzu_id", SqlDbType.VarChar,50),
					new SqlParameter("@ChizxFDzc_id", SqlDbType.Int,4)};
			parameters[0].Value = model.zu_name;
			parameters[1].Value = model.zu_pwd;
			parameters[2].Value = model.zu_sex;
			parameters[3].Value = model.zu_roles;
			parameters[4].Value = model.zu_weixin;
			parameters[5].Value = model.zu_qq;
			parameters[6].Value = model.zu_tel;
			parameters[7].Value = model.zu_email;
			parameters[8].Value = model.zu_lastdate;
			parameters[9].Value = model.zu_lastip;
			parameters[10].Value = model.zu_createdate;
			parameters[11].Value = model.zu_status;
            parameters[12].Value = model.zu_direct;
			parameters[13].Value = model.zu_id;
			parameters[14].Value = model.zc_id;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(string zu_id)
		{
			
			StringBuilder strSql=new StringBuilder();
            strSql.Append("delete from z_user,z_systemlog,z_summary,z_apply using z_user, z_systemlog, z_summary,z_apply ");
            strSql.Append(" where z_systemlog.zu_id=z_user.zu_id and z_summary.zu_id=z_user.zu_id and z_apply.zu_id=z_user.zu_id");
			strSql.Append(" and zu_id=@ChizxFDzu_id ");
			SqlParameter[] parameters = {
					new SqlParameter("@ChizxFDzu_id", SqlDbType.VarChar,50)	};
			parameters[0].Value = zu_id;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string idlist)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from z_user,z_systemlog,z_summary,z_apply using z_user, z_systemlog, z_summary,z_apply ");
            strSql.Append(" where z_systemlog.zu_id=z_user.zu_id and z_summary.zu_id=z_user.zu_id and z_apply.zu_id=z_user.zu_id");
            strSql.Append(" and zu_id in (" + idlist + ")");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


		/// <summary>
        /// 根据id和班级id得到一个对象实体
		/// </summary>
		public Chizx.Model.UserModel GetModel(string zu_id,int zc_id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 zu_id,zc_id,zu_name,zu_pwd,zu_sex,zu_roles,zu_weixin,zu_qq,zu_tel,zu_email,zu_lastdate,zu_lastip,zu_createdate,zu_status,zu_direct from z_user ");
			strSql.Append(" where zu_id=@ChizxFDzu_id and zc_id=@ChizxFDzc_id ");
			SqlParameter[] parameters = {
					new SqlParameter("@ChizxFDzu_id", SqlDbType.VarChar,50),
					new SqlParameter("@ChizxFDzc_id", SqlDbType.Int,4)			};
			parameters[0].Value = zu_id;
			parameters[1].Value = zc_id;

			Chizx.Model.UserModel model=new Chizx.Model.UserModel();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}

        public Chizx.Model.UserModel GetModelByID(string zu_id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 zu_id,zc_id,zu_name,zu_pwd,zu_sex,zu_roles,zu_weixin,zu_qq,zu_tel,zu_email,zu_lastdate,zu_lastip,zu_createdate,zu_status,zu_direct from z_user ");
            strSql.Append(" where zu_id=@ChizxFDzu_id ");
            SqlParameter[] parameters = {
					new SqlParameter("@ChizxFDzu_id", SqlDbType.VarChar,50)	};
            parameters[0].Value = zu_id;

            Chizx.Model.UserModel model = new Chizx.Model.UserModel();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Chizx.Model.UserModel DataRowToModel(DataRow row)
		{
			Chizx.Model.UserModel model=new Chizx.Model.UserModel();
			if (row != null)
			{
				if(row["zu_id"]!=null)
				{
					model.zu_id=row["zu_id"].ToString();
				}
				if(row["zc_id"]!=null && row["zc_id"].ToString()!="")
				{
					model.zc_id=int.Parse(row["zc_id"].ToString());
				}
				if(row["zu_name"]!=null)
				{
					model.zu_name=row["zu_name"].ToString();
				}
				if(row["zu_pwd"]!=null)
				{
					model.zu_pwd=row["zu_pwd"].ToString();
				}
				if(row["zu_sex"]!=null && row["zu_sex"].ToString()!="")
				{
					model.zu_sex=int.Parse(row["zu_sex"].ToString());
				}
				if(row["zu_roles"]!=null)
				{
					model.zu_roles=row["zu_roles"].ToString();
				}
				if(row["zu_weixin"]!=null)
				{
					model.zu_weixin=row["zu_weixin"].ToString();
				}
				if(row["zu_qq"]!=null)
				{
					model.zu_qq=row["zu_qq"].ToString();
				}
				if(row["zu_tel"]!=null)
				{
					model.zu_tel=row["zu_tel"].ToString();
				}
				if(row["zu_email"]!=null)
				{
					model.zu_email=row["zu_email"].ToString();
				}
				if(row["zu_lastdate"]!=null)
				{
					model.zu_lastdate=row["zu_lastdate"].ToString();
				}
				if(row["zu_lastip"]!=null)
				{
					model.zu_lastip=row["zu_lastip"].ToString();
				}
				if(row["zu_createdate"]!=null && row["zu_createdate"].ToString()!="")
				{
					model.zu_createdate=DateTime.Parse(row["zu_createdate"].ToString());
				}
				if(row["zu_status"]!=null)
				{
					model.zu_status=row["zu_status"].ToString();
				}
                if (row["zu_direct"] != null)
                {
                    model.zu_direct = row["zu_direct"].ToString();
                }
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select zu_id,zc_id,zu_name,zu_pwd,zu_sex,zu_roles,zu_weixin,zu_qq,zu_tel,zu_email,zu_lastdate,zu_lastip,zu_createdate,zu_status,zu_direct ");
			strSql.Append(" FROM z_user ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" zu_id,zc_id,zu_name,zu_pwd,zu_sex,zu_roles,zu_weixin,zu_qq,zu_tel,zu_email,zu_lastdate,zu_lastip,zu_createdate,zu_status,zu_direct ");
			strSql.Append(" FROM z_user ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM z_user ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.zc_id desc");
			}
			strSql.Append(")AS Row, T.*  from z_user T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@ChizxFDtblName", SqlDbType.VarChar, 255),
					new SqlParameter("@ChizxFDfldName", SqlDbType.VarChar, 255),
					new SqlParameter("@ChizxFDPageSize", SqlDbType.Int),
					new SqlParameter("@ChizxFDPageIndex", SqlDbType.Int),
					new SqlParameter("@ChizxFDIsReCount", SqlDbType.Bit),
					new SqlParameter("@ChizxFDOrderType", SqlDbType.Bit),
					new SqlParameter("@ChizxFDstrWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "z_user";
			parameters[1].Value = "zc_id";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  BasicMethod
		#region  ExtensionMethod

		#endregion  ExtensionMethod
	}
}

